package com.hzlx;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory;
import org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter;
import org.springframework.messaging.MessageChannel;

@Configuration
public class MQTTConfig {
    @Bean
    public MessageChannel mqttInputChannel() {
        return new DirectChannel();
    }
    @Value("${mqtt.broker-url}")
    String brokerUrl;
    @Value("${mqtt.client-id}")
    String clientId;
    @Value("${mqtt.topic}")
    String topic;
    @Bean
    public MqttPahoMessageDrivenChannelAdapter inbound() {
        MqttPahoMessageDrivenChannelAdapter adapter =
                new MqttPahoMessageDrivenChannelAdapter(
                        brokerUrl,
                        clientId,
                        topic);
        adapter.setOutputChannel(mqttInputChannel());
        return adapter;
    }
}
